//
//  GoodsInfoImageTableViewCell.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/1.
//

#import "GoodsInfoImageTableViewCell.h"

@interface GoodsInfoImageTableViewCell ()

@property (nonatomic, assign) CGFloat imageW;
@property (nonatomic, assign) CGFloat imageH;

@end

@implementation GoodsInfoImageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    [self.contentView addSubview:self.imageV];
    [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.trailing.mas_equalTo(-16);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-10);
    }];
    
    [self.contentView addSubview:self.playImageV];
    [self.playImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(60);
        make.center.mas_equalTo(0);
    }];
}

- (void)setImageUrl:(NSString *)imageUrl {
    _imageUrl = imageUrl;
    CGFloat w = kWidthForScreen-32;
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"avatar"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image.size.width <= w) {
            self.imageW = image.size.width;
            self.imageH = image.size.height;
        }else {
            CGFloat s = image.size.width / w;
            self.imageW = w;
            self.imageH = image.size.height / s;
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.imageV mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(self.imageW, self.imageH));
            }];
        });
    }];
}

- (UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] init];
        _imageV.contentMode = UIViewContentModeScaleAspectFill;
        _imageV.layer.cornerRadius = 10;
        _imageV.layer.masksToBounds = YES;
    }
    return _imageV;
}

- (UIImageView *)playImageV {
    if (!_playImageV) {
        _playImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shopinfo_play"]];
        _playImageV.hidden = YES;
    }
    return _playImageV;
}


@end
